<?php

Yii::import('application.models._base.BaseGalleryItems');

class GalleryItems extends BaseGalleryItems
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'category' => array(self::BELONGS_TO, 'GalleryCategory', 'id'),
		);
	}
	public function getTotals($catid)
	{
		$sql="SELECT count(id) as num FROM gallery_items WHERE catid=:p";
		$command = Yii::app()->db->createCommand($sql);
		$command->bindParam(':p', $catid);
		return $command->queryScalar();
	}
	public function getItems($catid,$limit='',$offset='')
	{
		$limit = ($limit!='')?" LIMIT $limit ":"";
		$offset = ($offset!='')?" OFFSET $offset ":"";
		$sql = "SELECT gi.title, gi.id, gi.image, gi.description
				FROM gallery_items as gi
				LEFT JOIN gallery_category as gc ON gc.id=gi.catid
				WHERE catid=:p
				$limit
				$offset
		";
		$command = Yii::app()->db->createCommand($sql);
		$command->bindParam(':p', $catid);
		return $command->queryAll();
	}
}